package com.kyzh.core.activities

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.widget.EditText
import android.widget.ImageView
import androidx.core.content.ContextCompat
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.impls.UserImpl
import com.jeremyliao.liveeventbus.LiveEventBus
import com.kyzh.core.R
import com.gushenge.core.toast
import kotlinx.android.synthetic.main.activity_password.*
import kotlinx.android.synthetic.main.activity_password.etAccount
import kotlinx.android.synthetic.main.activity_password.etPassword

class PasswordActivity  : BaseActivity(){
    companion object{
        @JvmStatic
        fun start(context: Activity, token:String?,type :Int) {
            val starter = Intent(context, PasswordActivity::class.java).putExtra("token",token).putExtra("type",type)
            context.startActivityForResult(starter,1000)
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_password)

        btLogin.setOnClickListener {
            val token = intent.getStringExtra("token")
            val type = intent.getIntExtra("type",0)
            val password = etAccount.text.toString().trim()
            val invite = etPassword.text.toString().trim()
            if(type == 1){
                UserImpl.loginByQqWx(token, "1",password,invite) {
                    if(code == 1){
                        MMKVConsts.uid= this.data.toString()
                        LiveEventBus.get<Boolean>("login").post(true)
                        setResult(RESULT_OK)
                        finish()
                    }else{
                        message.toast()
                    }
                }
            }else if(type == 2){
                UserImpl.loginByQqWx(token, "2",password,invite) {
                    if(code == 1){
                        MMKVConsts.uid= this.data.toString()
                        LiveEventBus.get<Boolean>("login").post(true)
                        setResult(RESULT_OK)
                        finish()
                    }else{
                        message.toast()
                    }
                }
            }else{
                UserImpl.setPassword(token,password,invite){
                    if(code == 1){
                        MMKVConsts.uid = data!!
                        LiveEventBus.get<Boolean>("login").post(true)
                        setResult(RESULT_OK)
                        finish()
                    }else{
                        message.toast()
                    }
                }
            }
        }
        ivDel.setOnClickListener {
            showPass(etAccount,ivDel)
        }
    }
    private fun showPass(editview: EditText,iv:ImageView) {
        if (editview.transformationMethod != HideReturnsTransformationMethod.getInstance()) {
            editview.transformationMethod = HideReturnsTransformationMethod.getInstance()
            iv.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.ic_login_showpassword))
        } else {
            editview.transformationMethod = PasswordTransformationMethod.getInstance()
            iv.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.ic_login_hidepassword))
        }
    }
}